Write-Host "Testing WorkflowCore functionality..." -ForegroundColor Green

$baseUrl = "http://localhost:8080"

# Test 1: Check if server is running
Write-Host "Test 1: Checking if server is running..." -ForegroundColor Cyan
try {
    $response = Invoke-WebRequest -Uri "$baseUrl" -Method GET -TimeoutSec 5
    Write-Host "Server is running!" -ForegroundColor Green
}
catch {
    Write-Host "Server is not responding: $($_.Exception.Message)" -ForegroundColor Red
    exit
}

# Test 2: Test login
Write-Host "`nTest 2: Testing login..." -ForegroundColor Cyan
$loginData = @{
    userName = "admin"
    password = "1q2w3E*"
} | ConvertTo-Json

$headers = @{
    "Content-Type" = "application/json"
}

try {
    $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/user-login/login" -Method POST -Body $loginData -Headers $headers
    if ($loginResponse.statusCode -eq 200) {
        Write-Host "Login successful!" -ForegroundColor Green
        $token = $loginResponse.token
        
        $authHeaders = @{
            "Authorization" = "Bearer $token"
            "Content-Type" = "application/json"
        }
        
        # Test 3: Test workflow instances
        Write-Host "`nTest 3: Testing workflow instances..." -ForegroundColor Cyan
        try {
            $instancesResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/instances" -Method GET -Headers $authHeaders
            Write-Host "Workflow instances API working!" -ForegroundColor Green
            Write-Host "Response: $($instancesResponse | ConvertTo-Json)" -ForegroundColor Yellow
        }
        catch {
            Write-Host "Workflow instances API failed: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # Test 4: Test pending approvals
        Write-Host "`nTest 4: Testing pending approvals..." -ForegroundColor Cyan
        try {
            $pendingResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/pending-approvals" -Method GET -Headers $authHeaders
            Write-Host "Pending approvals API working!" -ForegroundColor Green
            Write-Host "Response: $($pendingResponse | ConvertTo-Json)" -ForegroundColor Yellow
        }
        catch {
            Write-Host "Pending approvals API failed: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # Test 5: Test creating inbound order
        Write-Host "`nTest 5: Testing inbound order creation..." -ForegroundColor Cyan
        $inboundData = @{
            type = "采购入库"
            date = "2024-01-01T00:00:00"
            state = 2
            warehouseid = "00000000-0000-0000-0000-000000000001"
            inventorydetailsid = "00000000-0000-0000-0000-000000000001"
            manager = "00000000-0000-0000-0000-000000000001"
            vendor = "测试供应商"
            contact = "测试联系人"
            phone = "13800138000"
            remark = "WorkflowCore test inbound order"
            createInventoryDetailsInput = @(
                @{
                    commoditiesId = "00000000-0000-0000-0000-000000000001"
                    positionid = "00000000-0000-0000-0000-000000000001"
                    num = 10
                }
            )
        } | ConvertTo-Json -Depth 3
        
        try {
            $inboundResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/inboundmanagement/create-inbound" -Method POST -Body $inboundData -Headers $authHeaders
            Write-Host "Inbound order creation successful!" -ForegroundColor Green
            Write-Host "Response: $($inboundResponse | ConvertTo-Json)" -ForegroundColor Yellow
        }
        catch {
            Write-Host "Inbound order creation failed: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    else {
        Write-Host "Login failed: $($loginResponse.message)" -ForegroundColor Red
    }
}
catch {
    Write-Host "Login test failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`nTest completed!" -ForegroundColor Green 